import Vue from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
// 导入Element-UI
import eui from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
// 导入Axios
import axios from "axios";
//导入qs
import qs from 'qs';
// 导入dayjs
import dayjs from 'dayjs';
// 使用Element-UI
Vue.use(eui);
//使用qs
Vue.prototype.qs = qs;
// 使用dayjs
Vue.prototype.dayjs = dayjs;
Vue.config.productionTip = false;
//使用Axios
Vue.prototype.axios = axios;
Vue.prototype.$ajax = axios;
//使用jsonp
import { VueJsonp } from 'vue-jsonp'
Vue.use(VueJsonp)
axios.defaults.withCredentials = true;
axios.defaults.baseURL = 'http://127.0.0.1:9080';
//webSocket路径
let webUrl = '127.0.0.1:9080';
Vue.prototype.webUrl=webUrl;
Vue.prototype.$http=async function (url,method,data,fun){
  axios({
    url:url,
    method:method,
    data:data,
    headers:{
      "token":localStorage.getItem("token")
    }
  }).then((response)=> {
    let data = response.data
    //未登录
    if(data.code===40100||data.code===40400){
      //弹出未登录提示
      this.$message({
        message: data.msg,
        type: 'error',
        duration: 1200
      });
      //跳转登录
      setTimeout(()=> {
        localStorage.removeItem("token");
        this.$router.push("/login");
      },2000);
    }else if(data.code===20000){
      if(data.hasOwnProperty("token")){
        localStorage.setItem("token",data.token);
      }
      fun(response);
    }else {
      this.$message({
        message: data.msg,
        type: 'error',
        duration: 1200
      });
    }
  }).catch((error)=> {
    console.log(error);
  });
};
function padZero(num) {
  return num < 10 ? '0' + num : num;
}
Vue.prototype.formatTime=function(dateString){
  // 创建日期对象
  const date = new Date(dateString);

  // 获取年、月、日、时、分、秒
  const year = date.getFullYear();
  const month = padZero(date.getMonth() + 1);
  const day = padZero(date.getDate());
  const hours = padZero(date.getHours());
  const minutes = padZero(date.getMinutes());
  const seconds = padZero(date.getSeconds());

  // 拼接成需要的格式
  const formattedDate = year + '-' + month + '-' + day + ' ' + hours + ':' + minutes + ':' + seconds;

  // 返回格式化后的日期字符串
  return formattedDate;
}
Vue.prototype.parseStringToArray = function (target,splitFlag){
  let array;
  if(typeof (target)!="string"){
    this.$message({
      message: '浏览器无法解析，请重试',
      type: 'error',
      duration: 1200
    });
  }
  try {
    array = target.split(splitFlag);
    return array;
  } catch (e) {
    this.$message({
      message: '浏览器无法解析，请重试',
      type: 'error',
      duration: 1200
    });
  }
}
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
